﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;

public partial class Giamsatdinhky_giamsatnuocthai1 : System.Web.UI.Page
{
    public int i = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Employee"] == null && Session["Admin"] == null)
            Response.Redirect("../login.aspx");
        if (!IsPostBack)
        {
            //Setting Selected index prior to binding RadGrid:
            //If the index is in detail table, parent items will be expanded
            //automatically
            RadGrid_gsat.SelectedIndexes.Add(1, 0, 1);
            //Index of 1, 0, 1, 0, 0 means:
            //1 - item with index 1 in the MasterTabelView
            //0 - detail table with index 0
            //1 - item with index 1 (the second item) in the first detail table
            //0 - 0 the third-level detail table
            //0 - the item with index 0 in the third-level table
        }
    }
    protected void RadGrid_gsat_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
    {
        string item = getItemName(e.Item.OwnerTableView.Name);
        string field = getFieldName(e.Item.OwnerTableView.Name);
        if (e.Exception != null)
        {
            e.KeepInEditMode = true;
            e.ExceptionHandled = true;
            DisplayMessage(item + " " + e.Item[field].Text + " cannot be updated. Reason: " + e.Exception.Message);
        }
        else
        {
            DisplayMessage(item + " " + e.Item[field].Text + " updated");
        }
    }

    protected void RadGrid_gsat_ItemInserted(object source, GridInsertedEventArgs e)
    {
        string item = getItemName(e.Item.OwnerTableView.Name);
        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            DisplayMessage(item + " cannot be inserted. Reason: " + e.Exception.Message);
        }
        else
        {
            DisplayMessage(item + " inserted");
        }
    }

    protected void RadGrid_gsat_ItemDeleted(object source, GridDeletedEventArgs e)
    {
        string item = getItemName(e.Item.OwnerTableView.Name);
        string field = getFieldName(e.Item.OwnerTableView.Name);
        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            DisplayMessage(item + " " + e.Item[field].Text + " cannot be deleted. Reason: " + e.Exception.Message);
        }
        else
        {
            DisplayMessage(item + " " + e.Item[field].Text + " deleted");
        }
    }

    protected void RadGrid_gsat_InsertCommand(object source, GridCommandEventArgs e)
    {
       
       if ("Chitietgs".Equals(e.Item.OwnerTableView.Name))
        {
            GridDataItem parentItem = (GridDataItem)e.Item.OwnerTableView.ParentItem;
            SqlDataSource_ct.InsertParameters["Mags"].DefaultValue = parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["Mags"].ToString();
        }
    }

    private String getItemName(string tableName)
    {
        switch (tableName)
        {
         
            case ("Giamsatnuocthai"):
                {
                    return "Giamsatnuocthai";
                }
            case ("Details"):
                {
                    return "Details for giamsat";
                }
            default: return "";
        }
    }

    private String getFieldName(string tableName)
    {
        switch (tableName)
        {
            case ("Giamsatnuocthai"):
                {
                    return "Mags";
                }
            case ("Chitietgs"):
                {
                    return "Mags";
                }
            
            default: return "";
        }
    }

    private void DisplayMessage(string text)
    {
        RadGrid_gsat.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
    }
    protected void createWhereString()
    {
        string whStr = "";
        if (RadComboBox_dotgs.Text != "")
        {

            if (whStr != "")
                whStr += " AND Madotgs =" + RadComboBox_dotgs.SelectedValue + "";
            else
                whStr = " WHERE Madotgs =" + RadComboBox_dotgs.SelectedValue + "";
        }
        if (RadComboBox_dn.Text != "")
        {
            if (whStr != "")
                whStr += " AND Madnghiep =" + RadComboBox_dn.SelectedValue + "";
            else
                whStr = " WHERE Madnghiep =" + RadComboBox_dn.SelectedValue + "";
        }
        SqlDataSource_gsnuocthai.SelectCommand = "SELECT * FROM [Giamsatnuocthai] " + whStr + "  Order By Mags DESC"; RadGrid_gsat.Rebind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        createWhereString();
    }
}
